package com.xpgk.mvc.budgetYear;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantRender;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.tools.ToolRandoms;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service(name = BudgetYearService.serviceName)
public class BudgetYearService extends  BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(BudgetYearService.class);
	public static final String serviceName = "budgetYearService";
	/**保存预算
	 * 
	 * @param bdList
	 * @param organIds
	 * @param userIds
	 * @return
	 */
	public  String  saveBudget(String oldNum,String nowNum,String nextNum,String  budgetJson,String organIds,String userIds)
	{	
		String  msg=ConstantRender.render_success_code;
		  Calendar c=Calendar.getInstance();
		JSONArray budgetjson = JSONArray.fromObject(budgetJson);
		//本年预算记录
		Budget  bg=Budget.dao.getBudget(organIds, c.get(Calendar.YEAR)+"");
		if(bg!=null)
		{	
			//更新本年预算记录
			bg.set(Budget.column_type, "1")
			.set(Budget.column_changeUserids, userIds)
			.set(Budget.column_check_state, "2")
			.set(Budget.column_submit_state, "1")
			.set(Budget.column_gxsj, new Date());
			if(!bg.update())
			{
				log.debug("更新本年预算记录失败");
				throw new RuntimeException("更新本年预算记录失败!");
			}
			if (budgetjson.size() > 0) 
			{
				for (int i = 0; i < budgetjson.size(); i++) {
					JSONObject budgetJob = budgetjson.getJSONObject(i);
					BudgetDetail bd=BudgetDetail.dao.getBudgetDetail(bg.getStr(Budget.column_ids), budgetJob.get("proids").toString());
					if(bd!=null)
					{	
						//更新本年10，11，12月份预计数量以及全年总的数量和月平均数量
						bd.set(BudgetDetail.column_monthNum, budgetJob.get("nowmonthwillpurchased"))
							.set(BudgetDetail.column_yearSum, budgetJob.get("nowyearwillpurchased"))
							.set(BudgetDetail.column_octoberNum,budgetJob.get("now10willpurchased") )
							.set(BudgetDetail.column_novemberNum,budgetJob.get("now11willpurchased") )
							.set(BudgetDetail.column_decemberNum,budgetJob.get("now12willpurchased") )
							.set(BudgetDetail.column_gxsj, new Date());
						if(!bd.update())
						{
							log.debug("更新本年10，11，12月份预计数量以及全年总的数量和月平均数量失败");
							throw new RuntimeException("更新本年10，11，12月份预计数量以及全年总的数量和月平均数量失败!");
						}
					}else
					{
						//如果产品记录是空（上次记录没有，本次更新记录有）
						BudgetDetail  newbd=new BudgetDetail();
						//保存这个产品的预算明细
						newbd.set(BudgetDetail.column_budgetIds, bg.getStr(Budget.column_ids))
							.set(BudgetDetail.column_productIds, budgetJob.get("proids"))
							.set(BudgetDetail.column_monthNum, budgetJob.get("nowmonthwillpurchased"))
							.set(BudgetDetail.column_yearSum, budgetJob.get("nowyearwillpurchased"))
							.set(BudgetDetail.column_octoberNum,budgetJob.get("now10willpurchased") )
							.set(BudgetDetail.column_novemberNum,budgetJob.get("now11willpurchased") )
							.set(BudgetDetail.column_decemberNum,budgetJob.get("now12willpurchased") )
							.set(BudgetDetail.column_gxsj, new Date());
							if(!newbd.save())
							{
								log.debug("保存本年10，11，12月份预计数量以及全年总的数量和月平均数量失败");
								throw new RuntimeException("保存本年10，11，12月份预计数量以及全年总的数量和月平均数量失败!");
							}
					}
				}
			}
			
		}
		//下一年的预算记录和预算明细
		Budget  nextbg=Budget.dao.getBudget(organIds, (c.get(Calendar.YEAR)+1)+"");
		if(nextbg!=null)
		{	
			//更新下一年预算
			nextbg.set(Budget.column_type, "1")
					.set(Budget.column_lastYearNum, oldNum)
					.set(Budget.column_nowYearNum, nowNum)
					.set(Budget.column_nextYearNum, nextNum)
					.set(Budget.column_changeUserids, userIds)
					.set(Budget.column_check_state, "2")
					.set(Budget.column_submit_state, "1")
					.set(Budget.column_gxsj, new Date());
			if(!nextbg.update())
			{
				log.debug("更新下一年年预算记录失败");
				throw new RuntimeException("更新下一年年预算记录失败!");
			}
			if (budgetjson.size() > 0) 
			{
				for (int i = 0; i < budgetjson.size(); i++) {
					JSONObject budgetJob = budgetjson.getJSONObject(i);
					BudgetDetail bd=BudgetDetail.dao.getBudgetDetail(nextbg.getStr(Budget.column_ids), budgetJob.get("proids").toString());
					if(bd!=null)
					{	
						//更新下一年预算明细
						bd.set(BudgetDetail.column_monthNum, budgetJob.get("nextmonthwillpurchased"))
							.set(BudgetDetail.column_yearSum, budgetJob.get("nextyearwillpurchased"))
							.set(BudgetDetail.column_januaryNum, budgetJob.get("january"))//一月
							.set(BudgetDetail.column_februaryNum, budgetJob.get("february"))//2月
							.set(BudgetDetail.column_marchNum, budgetJob.get("march"))//3月
							.set(BudgetDetail.column_aprilNum,  budgetJob.get("april"))//4月
							.set(BudgetDetail.column_mayNum, budgetJob.get("may"))//5月
							.set(BudgetDetail.column_juneNum, budgetJob.get("june"))//6月
							.set(BudgetDetail.column_julyNum, budgetJob.get("july"))//7月
							.set(BudgetDetail.column_augustNum, budgetJob.get("august"))//8月
							.set(BudgetDetail.column_septemberNum, budgetJob.get("september"))//9月
							.set(BudgetDetail.column_octoberNum,budgetJob.get("october") )//10月
							.set(BudgetDetail.column_novemberNum,budgetJob.get("november") )//11月
							.set(BudgetDetail.column_decemberNum,budgetJob.get("december") )//12月
							.set(BudgetDetail.column_gxsj, new Date());
						if(!bd.update())
						{
							log.debug("更新下一年预算明细失败");
							throw new RuntimeException("更新下一年预算明细失败!");
						}
					}
				}
			}
			
		}else
		{
			//保存下一年预算
			Budget  newnextbg =new Budget();
			newnextbg.set(Budget.column_organIds, organIds)
					.set(Budget.column_lastYearNum, oldNum)
					.set(Budget.column_nowYearNum, nowNum)
					.set(Budget.column_nextYearNum, nextNum)
					.set(Budget.column_time, new Date())
					.set(Budget.column_year, c.get(Calendar.YEAR)+1)
					.set(Budget.column_type, "0")
					.set(Budget.column_userID, userIds)
					.set(Budget.column_check_state, "2")
					.set(Budget.column_submit_state, "1")
					.set(Budget.column_gxsj, new Date());
			if(!newnextbg.save())
			{
				log.debug("保存下一年年预算记录失败");
				throw new RuntimeException("保存下一年年预算记录失败!");
			}
			if (budgetjson.size() > 0) 
			{
				for (int i = 0; i < budgetjson.size(); i++) {
					JSONObject budgetJob = budgetjson.getJSONObject(i);
					BudgetDetail  bd=new BudgetDetail();
						//保存下一年预算明细
						bd.set(BudgetDetail.column_budgetIds, newnextbg.getStr(Budget.column_ids))
							.set(BudgetDetail.column_productIds, budgetJob.get("proids"))
							.set(BudgetDetail.column_monthNum, budgetJob.get("nextmonthwillpurchased"))
							.set(BudgetDetail.column_yearSum, budgetJob.get("nextyearwillpurchased"))
							.set(BudgetDetail.column_januaryNum, budgetJob.get("january"))//一月
							.set(BudgetDetail.column_februaryNum, budgetJob.get("february"))//2月
							.set(BudgetDetail.column_marchNum, budgetJob.get("march"))//3月
							.set(BudgetDetail.column_aprilNum,  budgetJob.get("april"))//4月
							.set(BudgetDetail.column_mayNum, budgetJob.get("may"))//5月
							.set(BudgetDetail.column_juneNum, budgetJob.get("june"))//6月
							.set(BudgetDetail.column_julyNum, budgetJob.get("july"))//7月
							.set(BudgetDetail.column_augustNum, budgetJob.get("august"))//8月
							.set(BudgetDetail.column_septemberNum, budgetJob.get("september"))//9月
							.set(BudgetDetail.column_octoberNum,budgetJob.get("october") )//10月
							.set(BudgetDetail.column_novemberNum,budgetJob.get("november") )//11月
							.set(BudgetDetail.column_decemberNum,budgetJob.get("december") )//12月
							.set(BudgetDetail.column_gxsj, new Date());
						if(!bd.save())
						{
							log.debug("保存下一年预算明细失败");
							throw new RuntimeException("保存下一年预算明细失败!");
						}
					
				}
			}
			
		}
		return  msg;
	}
	

}
